package shy.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import shy.bean.User;
import shy.mapper.RoleMapper;
import shy.mapper.UserMapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
@RestController
@RequestMapping("/user")

public class UserController {
    @Resource
    private UserMapper mapper;
    @Resource
    private RoleMapper roleMapper;

    //@RequestParam(defaultValue = "1")当page未提供时，默认值为1
    //    查询方法
    @RequestMapping("/find")
    public PageInfo<User> find(@RequestParam(defaultValue = "1") int page) {

//        引入分页操作
        PageHelper.startPage(page, 10);
//        全查询
        List<User> list = mapper.findAllUser();
        //        拆箱装箱
        for (int i = 0; i < list.size(); i++) {
            //获取每一个人
            User one = list.get(i);
            Integer roleId = one.getUserRole();
            String roleName = roleMapper.findRoleNameByRoleId(roleId);
            one.setRoleName(roleName);
        }
        //返回分页信息：当前页数据、总条数、总页数、上一页、下一页等
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //    删除方法
    @RequestMapping("/delete")
    public int delete(int id) {
        return mapper.deleteUserById(id);
    }

    //    条件搜索
    @RequestMapping("/find_name")
    public PageInfo<User> findUserByUserNameAndUserRole(String userName, int userRole, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        PageHelper.startPage(page, size);
        List<User> list = mapper.findUserByNameAndRole("%" + userName + "%", userRole);
        for (User one : list) {
            int roleId = one.getUserRole();
            String roleName = roleMapper.findRoleNameByRoleId(roleId);
            one.setRoleName(roleName);
        }
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @RequestMapping("/save")
    public int save(User user) {
        return mapper.saveUser(user);
    }

    @RequestMapping("/update")
    public int update(User user) {
        return mapper.updateUser(user);
    }


    @RequestMapping("/login")
    public String login(String userCode, String userPassword, HttpSession session){
        User user=mapper.login(userCode,userPassword);

//    账号或者密码错误
        if (user==null){
            return "你的账号或者密码错误";
        }
//    账号密码正确
//    保存到session容器中
        session.setAttribute("USER",user);
        return "success";
    }
    @RequestMapping("/update_pass")
    public int changPass(String password,HttpSession session){
//        从容器中获取到该用户
        Object obj=session.getAttribute("USER");
        if (obj==null){
            return  -1;//我认为-1代表未登录
        }
        User user = (User) obj;
        int row = mapper.updateUserPassword(user.getId(), password);
        return row;
    }
    @RequestMapping("/logout")
    public int logout(HttpSession session){
        session.removeAttribute("USER");
        return  1;
    }
}